// Copyright (c) 2008, 
// Marc Speck, http://www.faindu.com
// All Rights Reserved. The following is Source Code and is subject to all restrictions 
// on such code as contained in the license accompanying this product.
package com.faindu.editImage.crop
{
	import com.faindu.editImage.base.BaseControl;
	import com.faindu.editImage.base.BaseTool;
	
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
/**
 * The user drags these "buttons" to edit the image.
 */
public class CropControl extends BaseControl
{
	/**
	 * Constructor
	 */
	function CropControl( interactionMethod:Function, referenceName:String ) 
	{
		super( interactionMethod, referenceName )
		cursor = new CropCursor( referenceName )
	}

	protected const LINE_THICKNESS: Number = 2
	protected const BACKGROUND_COLOR: uint = 0x222222
	protected const LINE_COLOR: uint = 0xf8f8f8
	
	/**
	 * @inheritDoc
	 */	
	override public function draw( event:Event=null ): void 
	{
		super.draw()
		
		graphics.clear()
		graphics.lineStyle( LINE_THICKNESS, LINE_COLOR )
		graphics.beginFill( BACKGROUND_COLOR )
		
		const size: Number  = editImage.controlSize * 1.4
		const size2: Number = size/2
		const sizeGolden: Number = size/2.4

		var rotationCenter: Point
		
		switch ( referenceName )
		{
			case BaseTool.LEFT:
				graphics.drawRect( 0, -size2, sizeGolden, size )
				rotationCenter = editImage.controlsPositions.left
				graphics.lineStyle()							// invisible square for larger mouse target
				graphics.beginFill(0,0)
				graphics.drawRect( 0, -size2, size, size )
				break;
				
			case BaseTool.TOP:
				graphics.drawRect( -size2, 0, size, sizeGolden )
				rotationCenter = editImage.controlsPositions.top
				graphics.lineStyle()							// invisible square for larger mouse target
				graphics.beginFill(0,0)
				graphics.drawRect( -size2, 0, size, size )				
				break;
				
			case BaseTool.RIGHT:
				graphics.drawRect( -sizeGolden, -size2, sizeGolden, size )
				rotationCenter = editImage.controlsPositions.right
				graphics.lineStyle()							// invisible square for larger mouse target
				graphics.beginFill(0,0)
				graphics.drawRect( -size, -size2, size, size )				
				break;
				
			case BaseTool.BOTTOM:
				graphics.drawRect( -size2, -sizeGolden, size, sizeGolden )
				rotationCenter = editImage.controlsPositions.bottom
				graphics.lineStyle()							// invisible square for larger mouse target
				graphics.beginFill(0,0)
				graphics.drawRect( -size2, -size, size, size )						
				break;
				
			case BaseTool.TOP_LEFT:
				graphics.drawRect( 0, 0, size, sizeGolden )			// horizontal bar with border
				graphics.beginFill( BACKGROUND_COLOR )			// vertical bar with border
				graphics.drawRect( 0, 0, sizeGolden, size )
				graphics.beginFill( BACKGROUND_COLOR )			// overpaint the border
				graphics.lineStyle()
				graphics.drawRect( 1, 1, size-LINE_THICKNESS, sizeGolden-LINE_THICKNESS )
				graphics.beginFill(0,0) 						// invisible square for larger mouse target
				graphics.drawRect( 0, 0, size, size )
				
				rotationCenter = editImage.controlsPositions.topLeft
				break;
			
			case BaseTool.TOP_RIGHT:
				graphics.drawRect( -size, 0, size, sizeGolden )  	// horizontal bar with border
				graphics.beginFill( BACKGROUND_COLOR )			// vertical bar with border
				graphics.drawRect( -sizeGolden, 0, sizeGolden, size )
				graphics.beginFill( BACKGROUND_COLOR )			// overpaint the border
				graphics.lineStyle()
				graphics.drawRect( -size+1, 1, size-LINE_THICKNESS, sizeGolden-LINE_THICKNESS )
				graphics.beginFill(0,0)							// invisible square for larger mouse target
				graphics.drawRect( -size, 0, size, size )
				
				rotationCenter = editImage.controlsPositions.topRight
				break;
				
			case BaseTool.BOTTOM_RIGHT:
				graphics.drawRect( -size, -sizeGolden, size, sizeGolden )	// horizontal bar with border
				graphics.beginFill( BACKGROUND_COLOR )			// vertical bar with border
				graphics.drawRect( -sizeGolden, -size, sizeGolden, size )
				graphics.beginFill( BACKGROUND_COLOR ) 			// overpaint the border
				graphics.lineStyle()
				graphics.drawRect( -size+1, -sizeGolden+1, size-LINE_THICKNESS, sizeGolden-LINE_THICKNESS )
				graphics.beginFill(0,0) 						// invisible square for larger mouse target
				graphics.drawRect( -size, -size, size, size )
				
				rotationCenter = editImage.controlsPositions.bottomRight
				break;
				
			case BaseTool.BOTTOM_LEFT:
				graphics.drawRect( 0, -sizeGolden, size, sizeGolden )		// horizontal bar with border
				graphics.beginFill( BACKGROUND_COLOR )			// vertical bar with border
				graphics.drawRect( 0, -size, sizeGolden, size )
				graphics.beginFill( BACKGROUND_COLOR ) 			// overpaint the border
				graphics.lineStyle()
				graphics.drawRect( 1, -sizeGolden+1, size-LINE_THICKNESS, sizeGolden-LINE_THICKNESS )
				graphics.beginFill(0,0) 						// invisible square for larger mouse target
				graphics.drawRect( 0, -size, size, size )
				rotationCenter = editImage.controlsPositions.bottomLeft
				break;
		}
		graphics.endFill()
		
		rotateControl( rotationCenter )
	}
}
}